const { VIVO_AD_CONFIG } = require("../constants")

module.exports = {
    videoExist: false,
    videoFunc: null,
    interstitialExist: false,
    bannerDict: {},
    bannerLoad: [],
    nativeJson: null,           // 广告信息
    nativeCurrent: null,        // 当前广告
    nativeFuc: null,             // 广告函数
    nativeDict: {},             // 广告字典
    // nativeJson: {},
    init() {
        const sysInfo = qg.getSystemInfoSync();
        if (sysInfo.platformVersionCode < 1052) {
            qg.showToast({
                message: '平台版本过于成旧，请更新大于1052版本'
            })
            return
        }
        // this.getNativeJson(VIVO_AD_CONFIG.nativeAd[0])

        this.initVideo()
        this.loadInterstitial()
    },
    // 视频初始化
    initVideo() {
        if (!VIVO_AD_CONFIG.videoId) {
            return
        }
        this.ad_Video = qg.createRewardedVideoAd({ posId: VIVO_AD_CONFIG.videoId })
        this.ad_Video.onClose(res => {
            if (res && res.isEnded) {
                if (this.videoFunc) {
                    he.local.click("load_ad_get")
                    he.voice.pauseVoice(false)
                    this.videoFunc()
                    this.loadVideo()
                    this.videoFunc = null
                }
            }
        })
        this.loadVideo()
    },
    onLoadNative(res) {
        console.log('很成功很有精神', res)
        this.nativeJson = res.adList.pop()
        if (res && res.adList) {
            this.nativeJson = res.adList.pop()
            if (this.nativeJson) {
                this.nativeDict[this.nativeCurrent].reportAdShow({ adId: this.nativeJson.adId.toString() })
            }
        }
        if (this.nativeFuc) {
            this.nativeFuc(this.nativeJson)
        }
        // console.log(this.nativeDict)
    },

    onErrorNative(err) {
        if (err) {
            console.log(err)
        }
        if (this.nativeErrFuc) {
            this.nativeErrFuc()
        }
    },

    // 获得原生
    // 广告参数 ， 回调函数
    // retrun 结构表示已经有广告了
    // return true 表示等待回调
    // return false 不在配置内
    getNativeJson(id, fuc, errfuc) {
        if (this.nativeCurrent && this.nativeCurrent == id && this.nativeJson) {
            this.nativeDict[this.nativeCurrent].reportAdShow({ adId: this.nativeJson.adId.toString() })
            return this.nativeJson
        } else {
            this.nativeFuc = fuc
            this.nativeErrFuc = errfuc
            if (this.nativeDict[id]) {
                if (VIVO_AD_CONFIG.nativeAd.indexOf(id) < 0) {
                    return false
                } else {
                    this.nativeCurrent = id
                    this.nativeDict[this.nativeCurrent].load()
                    return true
                }
            } else {
                if (VIVO_AD_CONFIG.nativeAd.indexOf(id) < 0) {
                    return false
                } else {
                    this.nativeCurrent = id
                    let g = qg.createNativeAd({ posId: id })
                    if (!g.javaObject) {
                        return false
                    }
                    this.nativeDict[id] = g
                    // this.nativeDict[id] = qg.createNativeAd({ posId: id })
                    this.nativeDict[id].onLoad(this.onLoadNative.bind(this))
                    this.nativeDict[id].onError(this.onErrorNative.bind(this))
                    return true
                }
            }
        }
    },
    // 点击原生
    clickNative(id) {
        if (this.nativeCurrent && this.nativeCurrent == id && this.nativeJson) {
            this.nativeDict[id].reportAdClick({ adId: this.nativeJson.adId.toString() })
            this.nativeCurrent = null
            this.nativeJson = null
            he.log("点击广告", id)
        } else {
            he.warn("妹有广告", id)
        }
    },
    // 强行加载
    loadNativeAd(id) {
        if (this.nativeDict[id]) {
            this.nativeDict[id].load()
        }
    },
    // 加载视频
    loadVideo() {
        if (this.videoExist) {
            he.error("广告已存在")
            return
        }
        this.ad_Video.load().then((res) => {
            he.local.click("load_ad_suc")
            this.videoExist = true
        }).catch((error) => {
            this.videoExist = false
            this.loadVideo()
        })
    },
    // 显示视频
    showVideo(fuc) {
        if (!this.videoExist) {
            this.loadVideo()
            return
        }
        he.local.click("load_ad_click")
        he.voice.pauseVoice(true)
        this.videoFunc = fuc
        this.ad_Video.show()
        this.videoExist = false
    },
    // 加载插页
    loadInterstitial() {
        if (!VIVO_AD_CONFIG.interstitialId) {
            return
        }
        if (this.interstitialExist) {
            return
        }
        this.ad_Interstitial = qg.createInterstitialAd({ posId: VIVO_AD_CONFIG.interstitialId })
        this.ad_Interstitial.onLoad(() => {
            this.interstitialExist = true
        })
        this.ad_Interstitial.onError(err => {
            this.interstitialExist = false
        })

    },
    // 展示插页
    interstitialShow(func) {
        if (!this.interstitialExist) {
            this.loadInterstitial()
            return
        }
        this.ad_Interstitial.show()
        this.ad_Interstitial.onClose(() => {
            // console.log('把闹钟关罗')
            if (func) {
                func()
            }
        }, this)
    },
    // 展示Banner形式
    changeBanner(status, posNode, func) {
        let bannerId = posNode.name
        if (VIVO_AD_CONFIG.bannerId.indexOf(bannerId) < 0) {
            he.error("该广告节点妹有")
            return
        }
        if (status) {
            let posSize = cc.size(posNode.width * zoom_scale, posNode.height * zoom_scale);
            let frameSize = cc.view.getFrameSize();
            let winSize = cc.director.getWinSize();
            let worldPos = posNode.convertToWorldSpaceAR(cc.Vec2.ZERO)
            let width = posSize.width / winSize.width * frameSize.width;
            // let height = posSize.height / winSize.height * frameSize.height 
            // let left = (worldPos.x - posSize.width / 2) / winSize.width * frameSize.width
            let top = (winSize.height - worldPos.y - posSize.height / 2) / winSize.width * frameSize.width
            if (this.bannerDict[bannerId]) {
                this.bannerDict[bannerId].show()
            } else {
                this.bannerDict[bannerId] = qg.createBannerAd({
                    posId: bannerId, adIntervals: 30, style: {
                        left: 0,
                        top: top
                    }
                })
                this.bannerLoad.push(bannerId)
                this.bannerDict[bannerId].onLoad(() => {
                    let exit = this.bannerLoad.indexOf(bannerId)
                    if (exit >= 0) {
                        this.bannerLoad.splice(exit, 1)
                        this.bannerDict[bannerId].show()
                    }
                })
                this.bannerDict[bannerId].onError(err => {
                    let exit = this.bannerLoad.indexOf(bannerId)
                    if (exit >= 0) {
                        this.bannerLoad.splice(exit, 1)
                    }
                    he.error(err)
                })
                this.bannerDict[bannerId].onClose(() => {
                    if (func)
                        func()
                })
                if (width < 300) {
                    he.error("广告预制体小了")
                }
                this.bannerDict[bannerId].show()
            }
        } else {
            if (this.bannerDict[bannerId]) {
                this.bannerDict[bannerId].hide()
            } else {
                he.error("妹有对应广告")
            }
        }
    },

    closeBannerOnload() {
        if (this.bannerDict[VIVO_AD_CONFIG.bannerId[0]]) {
            this.bannerDict[VIVO_AD_CONFIG.bannerId[0]].offLoad(() => {
                console.log()
            })
            this.bannerDict[VIVO_AD_CONFIG.bannerId[0]].destroy()
        }
    },
}